package com.point.business.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;
import com.point.business.database.managers.InformationManager;
import com.point.business.database.managers.ScheduleManager;
import com.point.business.database.model.History;
import com.point.business.util.UTFUtil;

/**
 * Servlet implementation class LoginServlet
 */

public class AddToScheduleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		UTFUtil.utf8(request, response);
		PrintWriter out = response.getWriter();
		int userId = Integer.parseInt(request.getParameter("userId"));
		History historyResult = InformationManager.getInstance()
				.getLastHistory(userId);
		boolean isAdded = false;
		JsonObject jsono = new JsonObject();
		if (historyResult != null) {
			isAdded = ScheduleManager.getInstance().addSchedule(userId,
					historyResult.getSearchQuery(), true,
					historyResult.getSearchObject(),
					historyResult.getSearchDate());
		}
		if (isAdded == true) {
			jsono.addProperty("success", true);
			out.print(jsono);
		} else {
			jsono.addProperty("msg", "Duplicate schedule");
			out.print(jsono);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
